package com.yifeng.repo.base.utils.common;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.talkyun.utils.env.HostManager;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;

import java.util.Map;
import java.util.Properties;

/**
 * Created by daibing on 2023/3/18.
 */
public class BaseUtil {
    public static final Long OWNER_ID = IdWorker.getId();
    public static final String OWNER_NAME = HostManager.get().getHostName() + "-" + HostManager.get().getLocalIp();
    /**
     * Skywalking第一个版本直接拷贝TraceContext类到工程中，所以有TraceContext不一定有CallableWrapper，将来统一到EXIST_SKYWALKING
     */
    public static final boolean EXIST_SKYWALKING_WRAPPER = exists("org.apache.skywalking.apm.toolkit.trace.CallableWrapper");
    public static final boolean EXIST_SKYWALKING_TRACE = BaseUtil.exists("org.apache.skywalking.apm.toolkit.trace.TraceContext");

    public static String getSkywalkingTraceId() {
        if (EXIST_SKYWALKING_TRACE && isNotBlank(TraceContext.traceId())) {
            return TraceContext.traceId();
        }
        return "NO_TID";
    }

    public static boolean isBlank(String s) {
        return s == null || s.trim().length() == 0;
    }

    public static boolean isNotBlank(String s) {
        return !isBlank(s);
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return null == map || map.isEmpty();
    }

    public static String getEnvSettingIfPresent(String... key) {
        // first from vm
        Properties p = System.getProperties();
        for (@SuppressWarnings("rawtypes") Map.Entry en : p.entrySet()) {
            for (String k : key) {
                if (k.equalsIgnoreCase(en.getKey().toString())) {
                    return en.getValue().toString();
                }
            }
        }

        // second from env
        Map<String, String> env = System.getenv();
        for (@SuppressWarnings("rawtypes") Map.Entry en : env.entrySet()) {
            for (String k : key) {
                if (k.equalsIgnoreCase(en.getKey().toString())) {
                    return en.getValue().toString();
                }
            }
        }
        return null;
    }

    public static boolean exists(String clz) {
        try {
            Class.forName(clz);
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }

    public static String safeCut(String raw, int length) {
        if (raw == null) {
            return null;
        }
        if (raw.length() <= length) {
            return raw;
        }
        return raw.substring(0, length);
    }

    public static boolean equals(Map<?, ?> map1, Map<?, ?> map2) {
        if (map1 == map2) {
            return true;
        }
        if (map1 == null || map2 == null || map1.size() != map2.size()) {
            return false;
        }
        for (Object key : map1.keySet()) {
            Object value1 = map1.get(key);
            Object value2 = map2.get(key);
            if (value1 == null) {
                if (value2 != null || !map2.containsKey(key)) {
                    return false;
                }
            } else if (!value1.equals(value2)) {
                return false;
            }
        }
        return true;
    }
}
